import { sessionLocalStorage } from 'utils/session'
import { IAction } from './types'
import { ILoginResult } from 'apis/types/auth'

const session = sessionLocalStorage.getItem()

export interface IState {
  isLogined: boolean
  user?: ILoginResult
}
console.log('session::', session)
const store = {
  state: {
    isLogined: !!session.userId,
    user: session,
  },
  mutations: {
    login(state: IState, params: IAction) {
      sessionLocalStorage.setItem(params.payload?.user)
      state.user = params.payload?.user
      console.log('cookie', params.payload?.user?.cookie)
      // document.cookie = params.payload?.user?.cookie
      state.isLogined = true
    },
    logout(state: IState) {
      sessionLocalStorage.removeItem()
      state.user = undefined
      state.isLogined = false
    },
  },
  actions: {},
}

export default store
